//
//  ViewController.m
//  DYPrintDemo
//
//  Created by mr.ye on 2023/11/20.
//

#import "ViewController.h"
#import <DYPrintSDK/DYPrintSDK.h>
#import <CoreBluetooth/CoreBluetooth.h>
#import <DYPrintSDK/DYPrintSDK.h>
#import "ActionViewController.h"

@interface ViewController ()<UITableViewDelegate,UITableViewDataSource>
@property (weak, nonatomic) IBOutlet UITableView *list;
@property (strong, nonatomic)  NSMutableArray *listArray;
@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
   
    self.listArray =[NSMutableArray new];
    self.list.delegate = self;
    self.list.dataSource = self;
    [self.list registerClass:[UITableViewCell class] forCellReuseIdentifier:@"cell"];
    

    [[DYBLEManager sharedInstance]scanPrinters:10 call:^(NSDictionary* object) {
        CBPeripheral * p= object[@"peripheral"];
        if(![self.listArray containsObject:p]){
            [self.listArray addObject:p];
        }
        [self.list reloadData];
    } err:^(NSError * object) {
        
        
    }];

}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return self.listArray.count;
}
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 80;
}
-(UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    UITableViewCell * cell =[tableView dequeueReusableCellWithIdentifier:@"cell"];
    CBPeripheral * p= self.listArray[indexPath.row];
    cell.textLabel.text  = p.name;
    return cell;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    CBPeripheral * p= self.listArray[indexPath.row];
    [[DYBLEManager sharedInstance]connect:p timeout:5 call:^(CBPeripheral *  peripheral, NSError *  error) {
        if(!error){
            NSLog(@"连接成功了");
//            DYBLEManager *  ma =  [DYBLEManager sharedInstance];
//            ma.print = [[DYPrinterManager alloc]initWithPeripheral:peripheral];
//            
            
            [self.navigationController pushViewController:[ActionViewController new] animated:YES];
            
        }else{
            NSLog(@"%@",error.description);
        }
    }];
    
}
@end
